﻿
using Cl.Utils;
using Cl.Utils.Primitives.Class.UploadFile;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace Cl.Utils.Helper
{
    public static class UploadFileHelper
    {

        public static void DeleteFile(string path)
        {
            System.IO.File.Delete(path);
        }

        #region 私有方法


        /// <summary>
        /// 返回完整的网络地址,不带ip
        /// </summary>
        /// <param name="folder"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetWebUrl(string folder, string fileName)
        {
            return $"{GlobalService.FileConfigGlobal.RequestPath}\\{folder}\\{fileName}";
        }

        public static string GetNetworkPath(UploadType uploadType, string Id)
        {
            return $"{uploadType.ToString()}\\{Id}";
        }
        #endregion

        public static async Task<string> UploadOnlyOneFileAsync(IFormFile file, UploadType uploadType, string id)
        {
            DeleteFile(uploadType, id);
            return await UploadFileAsync(file, uploadType, id);
        }


        public static async Task<string> UploadFileAsync(IFormFile file, UploadType uploadType, string id)
        {
            var FolderPath = UploadFileHelper.GetNetworkPath(uploadType, id);
            var uploadsFolder = Path.Combine(GlobalService.FileConfigGlobal.UploadPath, FolderPath);

            //检测文件夹是否存在
            if (!Directory.Exists(uploadsFolder))
            {
                Directory.CreateDirectory(uploadsFolder);
            }

            var filePath = Path.Combine(uploadsFolder, file.FileName);
            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(fileStream);
            }

            return GetWebUrl(FolderPath, file.FileName);
        }

        public static void DeleteFile(UploadType uploadType, string id)
        {
            var FolderPath = UploadFileHelper.GetNetworkPath(uploadType, id);
            var uploadsFolder = Path.Combine(GlobalService.FileConfigGlobal.UploadPath, FolderPath);

            // 检查文件是否存在
            if (Directory.Exists(uploadsFolder))
            {
                // 图片扩展名列表，根据需要添加更多格式
                var fileList = Directory.GetFiles(uploadsFolder, "*.*", SearchOption.AllDirectories);
                foreach (string currentFile in fileList)
                {
                    string extension = Path.GetExtension(currentFile).ToLowerInvariant();

                    try
                    {
                        UploadFileHelper.DeleteFile(currentFile);
                    }
                    catch (IOException ex)
                    {
                        Console.WriteLine($"Error deleting file {currentFile}: {ex.Message}");
                    }
                }
            }
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="uploadType"></param>
        /// <param name="id"></param>
        /// <param name="fileName">如果文件名称有，则删除指定的文件</param>
        public static void DeleteByFileName(UploadType uploadType, string id, string fileName)
        {
            var FolderPath = UploadFileHelper.GetNetworkPath(uploadType, id);
            var uploadsFolderFile = Path.Combine(GlobalService.FileConfigGlobal.UploadPath, FolderPath, fileName);

            // 检查文件是否存在
            if (File.Exists(uploadsFolderFile))
            {
                string extension = Path.GetExtension(uploadsFolderFile).ToLowerInvariant();
                if (GlobalService.FileConfigGlobal.IsWhiteTypeData(extension))
                {
                    try
                    {
                        UploadFileHelper.DeleteFile(uploadsFolderFile);
                    }
                    catch (IOException ex)
                    {
                        Console.WriteLine($"Error deleting file {uploadsFolderFile}: {ex.Message}");
                    }
                }

                // 图片扩展名列表，根据需要添加更多格式
                //var fileList = Directory.GetFiles(uploadsFolderFile, "*.*", SearchOption.AllDirectories);
                //foreach (string currentFile in fileList)
                //{
                //    string extension = Path.GetExtension(currentFile).ToLowerInvariant();


                //}
            }
        }

        public static List<string> GetUploadsPathList(UploadType uploadType, string id)
        {
            List<string> result = new List<string>();

            var FolderPath = GetNetworkPath(uploadType, id);
            var uploadsFolder = Path.Combine(GlobalService.FileConfigGlobal.UploadPath, FolderPath);

            // 检查文件是否存在
            if (Directory.Exists(uploadsFolder))
            {
                // 图片扩展名列表，根据需要添加更多格式
                var fileList = Directory.GetFiles(uploadsFolder, "*.*", SearchOption.AllDirectories);
                foreach (string currentFile in fileList)
                {
                    string extension = Path.GetExtension(currentFile).ToLowerInvariant();
                    string fileName = Path.GetFileName(currentFile);

                    if (GlobalService.FileConfigGlobal.IsWhiteTypeData(extension))
                    {//如果是白名单数据，则进行添加；
                        result.Add(GetWebUrl(FolderPath, fileName));
                    }

                }
            }

            return result;
        }


        public static string GetUploadsPathData(UploadType uploadType, string id)
        {


            var FolderPath = GetNetworkPath(uploadType, id);
            var uploadsFolder = Path.Combine(GlobalService.FileConfigGlobal.UploadPath, FolderPath);

            // 检查文件是否存在
            if (Directory.Exists(uploadsFolder))
            {
                // 图片扩展名列表，根据需要添加更多格式
                var fileList = Directory.GetFiles(uploadsFolder, "*.*", SearchOption.AllDirectories);
                foreach (string currentFile in fileList)
                {
                    string extension = Path.GetExtension(currentFile).ToLowerInvariant();
                    string fileName = Path.GetFileName(currentFile);

                    if (GlobalService.FileConfigGlobal.IsWhiteTypeData(extension))
                    {//如果是白名单数据，则进行添加；
                        return GetWebUrl(FolderPath, fileName);
                    }
                }
            }

            return $"";
        }
    }
}
